<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 11/1/12
 * Time: 8:58 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
jimport('joomla.html.pagination');

/**
 * Congtrinhs Model
 */
class VastModelCongtrinhs extends JModelItem {
    protected $congtrinhs;
    private $_total = null;
    private $_pagination = null;

    public function __construct()
    {
        parent::__construct();
        
        $config = JVastConfig::getConfig();
        
        $this->setState('limit', (isset($config['numOfItemsPerPage']) ? (int)$config['numOfItemsPerPage'] : JRequest::getVar('limit', 5, '', 'int')));
        $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));
        $session = JFactory::getSession();

        $ten = JRequest::getString('congtrinhTen', $session->get('congtrinhs.ten'));
        $hosocanbo = JRequest::getInt('congtrinhHosocanbo', $session->get('congtrinhs.hosocanbo'));
                
        $session->set('congtrinhs.ten', $ten);
        $session->set('congtrinhs.hosocanbo', $hosocanbo);
                
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
            
        $query->select('a.*, b.hoten');
        $query->from('#__congtrinh AS a');
 
        if (!is_null($hosocanbo) && $hosocanbo != -1) {
            $query->where('a.hosocanbo_id = ' . (int)$hosocanbo);
        }
           
        if (!is_null($ten) && $ten !== '') 
            $query->where('a.ten like \'%' . $ten . '%\'');
        elseif ($ten === '')
            $query->where('a.ten like \'%\'');
        
        $query->join('INNER', '#__hosocanbo AS b ON a.hosocanbo_id = b.id');
        
        $db->setQuery($query);
        
        $this->congtrinhs = $db->loadObjectList();
            
        $this->_total = count($this->congtrinhs);          
    }

    /**
     * Get the message
     * @return string The message to be displayed to the user
     */
    public function getCongtrinhs()
    {
        $session = JFactory::getSession();
        $ten = $session->get('congtrinhs.ten');
        $hosocanbo = $session->get('congtrinhs.hosocanbo');
        
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $limit = $this->getState('limit');
        $limitstart = $this->getState('limitstart');

        $query->select('a.*, b.hoten');
        $query->from('#__congtrinh AS a');

        if (!is_null($hosocanbo) && $hosocanbo != -1) {
            $query->where('a.hosocanbo_id = ' . (int)$hosocanbo);
        }
            
        if (!is_null($ten) && $ten !== '') 
            $query->where('a.ten like \'%' . $ten . '%\'' . ' LIMIT ' . $limitstart . ',' . $limit);
        elseif ($ten === '')
            $query->where('a.ten like \'%\' LIMIT ' . $limitstart . ',' . $limit);

        $query->join('INNER', '#__hosocanbo AS b ON a.hosocanbo_id = b.id');
                    
        $db->setQuery($query);
        $this->congtrinhs = $db->loadObjectList();
                
        return $this->congtrinhs;
    }
    
    public function getPagination()
    {
        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        return $this->_pagination;
    }

    public function getTotal()
    {
        return $this->_total;
    }  
    
    public function getHosocanbo()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
                
        $query->select('a.*');
        $query->from('#__hosocanbo AS a');

        $db->setQuery($query);
        $hosocanbos = $db->loadObjectList();

        return $hosocanbos;        
    }
      
}